﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;
using Meraz.Core.Data;

namespace Meraz.Core.Repositories
{
    public class PostRepository : EFRepositoryBase<Post, int>, IPostRepository
    {
        public PostRepository() : base() { }
        public PostRepository(Data.IMerazContext context) : base(context) { }
        public override Post GetById(int id)
        {
            Post post = context.Posts
                .Where(where1 => where1.ID == id)
                .FirstOrDefault();
            FillAggregate(post);
            return post;
        }
        public Post GetByPermalink(string permalink)
        {
            Post post = context.Posts
                .Where(where1 => where1.Permalink == permalink)
                .FirstOrDefault();
            FillAggregate(post);
            return post;
        }

        private void FillAggregate(Post post)
        {
            if (post == null)
                return;
            context.LoadAggregate(post);
        }

        public override void Add(Post entity)
        {
            context.AddToPosts(entity);
//TODO: why?            context.Posts.AddObject(entity);
        }

        public override void Delete(Post entity)
        {
            context.Posts.DeleteObject(entity);
        }

        public override List<Post> All()
        {
            return context.Posts.ToList();
        }
    }
}
